package com.sduept.nwld.mobile.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionExpert;
import com.sduept.bigdata.equipment.assessment.service.InspectionExpertService;
import com.sduept.nwld.mobile.manager.MobileUtil;
import com.sduept.nwld.mobile.rest.impl.Rtn;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.entity.RoleEntityBean;
import com.sduept.permission.enums.RoleTypeEnum;
import com.sduept.permission.manager.PersonnelManager;
import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * app登录验证
 *
 */
@Slf4j
@RestController
@RequestMapping("/loginCheck")
public class LoginCheckService {
	 
	@Inject
	private PersonnelManager orgManager;
	
	@Inject
	private MobileUtil mobileUtil;//SQL注入检测类
	
	@Inject
	private InspectionExpertService inspectionExpertService;//评价专家
	
	private final String INSPECTION_ADMIN_ROLE_NAME="设备评估管理";
	
	
	/**
	 * @param params(不再验证APP4A账号，使用web端账号登录)
	 * @return
	 */
	@PostMapping("/login")
	public String loginCheck( @RequestBody String params){
		JSONObject p = JSONObject.parseObject(params);
		String userName=p.getString("userName");
		String password=p.getString("password");
		try {
			userName=mobileUtil.SQLInjectCheck(userName);
			password=mobileUtil.SQLInjectCheck(password);
		}catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		String result = orgManager.validateLogin(userName,password);
		return result;
	}
	
	/**
	 * 根据code获取角色
	 * @param params
	 * @return 登录人角色
	 */

	@PostMapping("/getRoleForInspection")
	public Rtn getRoleForInspection( @RequestBody String code){
		Rtn rtn = null;
		try {
			code=mobileUtil.SQLInjectCheck(code);
		}catch (Exception e) {
			e.printStackTrace();
			rtn=new Rtn("fail","",-1);
			return rtn;
		}
		//查找角色(管理员， 专家，其他)
		PersonnelEntityBean person=orgManager.findPersonnelByCode(code);
		InspectionAppUserInfo appUserInfo=new InspectionAppUserInfo();
		appUserInfo.setPersonCode(person.getCode());
		appUserInfo.setPersonName(person.getName());
		Set<RoleEntityBean> roles=person.getRoles();
		if(null!=roles) {
			for (RoleEntityBean roleEntityBean : roles) {
				if(RoleTypeEnum.system.toString().equals(roleEntityBean.getType())) {
					appUserInfo.setInspectionAdmin(true);
					break;
				}else if(INSPECTION_ADMIN_ROLE_NAME.equals(roleEntityBean.getName())) {
					appUserInfo.setInspectionExpert(true);
					break;
				}
			}
		}
		InspectionExpert expert=inspectionExpertService.findByCode(person.getCode());
		if(null!=expert) {
			appUserInfo.setInspectionExpert(true);
		}
		rtn=new Rtn(JSONObject.toJSONString(appUserInfo),null,1);
		return rtn;
	}
	
	/**
	 * 设备评估APP登录验证
	 * @param params
	 * @return 登录人角色
	 */

	@PostMapping("/loginForInspection")
	public Rtn loginForInspection( @RequestBody String params){
		JSONObject p = JSONObject.parseObject(params);
		String userName=p.getString("userName");
		String password=p.getString("password");
		Rtn rtn = null;
		try {
			userName=mobileUtil.SQLInjectCheck(userName);
			password=mobileUtil.SQLInjectCheck(password);
		}catch (Exception e) {
			e.printStackTrace();
			rtn=new Rtn("fail","",-1);
			return rtn;
		}
		String result = orgManager.validateLogin(userName,password);
		if(result=="success") {
			//登录成功,查找角色(管理，专家，其他)
			PersonnelEntityBean person=orgManager.findPersonnelByCode(userName);
			InspectionAppUserInfo appUserInfo=new InspectionAppUserInfo();
			appUserInfo.setPersonCode(person.getCode());
			appUserInfo.setPersonName(person.getName());
			Set<RoleEntityBean> roles=person.getRoles();
			if(null!=roles) {
				for (RoleEntityBean roleEntityBean : roles) {
					if(INSPECTION_ADMIN_ROLE_NAME.equals(roleEntityBean.getName())||RoleTypeEnum.system.toString().equals(roleEntityBean.getType())) {
						appUserInfo.setInspectionAdmin(true);
						break;
					}
				}
			}
			InspectionExpert expert=inspectionExpertService.findByCode(person.getCode());
			if(null!=expert) {
				appUserInfo.setInspectionExpert(true);
			}
			rtn=new Rtn(JSONObject.toJSONString(appUserInfo),null,1);
		}else {
			rtn=new Rtn(result,"登录失败！",-1);
		}
		return rtn;
	}
	/**
	 * 检测有效期，在有效期内可以使用该APP,暂时没有用，在前端进行验证
	 */

	@GetMapping("/checkValidDate")
	public String checkValidDate(){
		String validDateStr="2019-01-01 00:00:00";
		Calendar calendar=Calendar.getInstance();
		Date nowDate=calendar.getTime();
        Date validDate=DateUtil.getDateByString(validDateStr, DateUtil.pattern_ymdhms);	
        if(nowDate.before(validDate)) {
        	return "SUCCESS";
        }
		return "FAIL";
	}
	
	/**
	 * 用于APP测试的接口
	 * */

	@GetMapping("/test/{msg}")
	public String test(@PathVariable("msg") String msg) {
		log.info(msg);
		return "";
	}
	
	@SuppressWarnings("unused")
	private class InspectionAppUserInfo{
		private String personName;
		private String personCode;
		private boolean inspectionAdmin;//是否系统管理员
		private boolean inspectionExpert;//是否评价专家
		public String getPersonName() {
			return personName;
		}
		public void setPersonName(String personName) {
			this.personName = personName;
		}
		public boolean isInspectionAdmin() {
			return inspectionAdmin;
		}
		public void setInspectionAdmin(boolean inspectionAdmin) {
			this.inspectionAdmin = inspectionAdmin;
		}
		public boolean isInspectionExpert() {
			return inspectionExpert;
		}
		public void setInspectionExpert(boolean inspectionExpert) {
			this.inspectionExpert = inspectionExpert;
		}
		public String getPersonCode() {
			return personCode;
		}
		public void setPersonCode(String personCode) {
			this.personCode = personCode;
		}
	}
}
